using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Linq;

public partial class Home_ManageStudents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

        }
    }

    protected void gvStudentAttributes_RowCommand(int Index)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
        //   if (e.CommandName == "UpdateStudentAttribute")
        //  {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(Index);
        GridViewRow row = this.gvStudentAttributes.Rows[index];

        int studentAttribId = Int32.Parse(((TextBox)row.FindControl("txtHiddenLinkId")).Text);
        int userId = Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString());

        //Set a new expiry date
        DateTime dtNewExpiryDate = DateTime.Now.AddYears(1);
        //Try and find a new expiry date from the text box
        TextBox txtEditStudentAttributeExpiryDate = (TextBox)row.FindControl("txtEditStudentAttributeExpiryDate");
        if (txtEditStudentAttributeExpiryDate != null && txtEditStudentAttributeExpiryDate.Visible)
        {
            dtNewExpiryDate = Convert.ToDateTime(txtEditStudentAttributeExpiryDate.Text);
        }

        List<UserStudentAttribute> userStudentAttributes = (from usa1 in db.UserStudentAttributes
                                                            where usa1.StudentAttributeId == studentAttribId
                                                            && usa1.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                                                            orderby usa1.UserId
                                                            select usa1).ToList<UserStudentAttribute>();


        if (userStudentAttributes.Count > 0)//Update the existing row
        {
            UserStudentAttribute usa = new UserStudentAttribute();
            usa = userStudentAttributes.First<UserStudentAttribute>();
            usa.IsActive = ((CheckBox)row.FindControl("cbEditStudentAttribute")).Checked;
            usa.ExpiryDate = dtNewExpiryDate;
            usa.DateUpdated = DateTime.Now;
            usa.UpdatedBy = User.Identity.Name;

        }
        else if (((CheckBox)row.FindControl("cbEditStudentAttribute")).Checked == true)//insert a new row
        {
            UserStudentAttribute usa = new UserStudentAttribute();
            PlacementManager.LinkUserStudentAttribute oNewLinkUSA = new PlacementManager.LinkUserStudentAttribute();
            usa.CreatedBy = User.Identity.Name;
            usa.DateCreated = DateTime.Now;
            usa.DateUpdated = DateTime.Now;
            usa.ExpiryDate = dtNewExpiryDate;
            usa.IsActive = ((CheckBox)row.FindControl("cbEditStudentAttribute")).Checked;
            usa.UpdatedBy = User.Identity.Name;
            usa.UserId = userId;
            usa.StudentAttributeId = studentAttribId;
            db.UserStudentAttributes.InsertOnSubmit(usa);
        }
        db.SubmitChanges();
    }
    protected void gvStudentAttributes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (this.gvStudents.Rows.Count > 0)
            {
                int studentAttribId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "StudentAttributeID"));


                //Find out if there is an expiry date and show or hide the expiry date control
                TextBox txtEditStudentAttributeExpiryDate = (TextBox)e.Row.FindControl("txtEditStudentAttributeExpiryDate");
                if (txtEditStudentAttributeExpiryDate != null)
                {


                    List<StudentAttribute> studentAttributes = (from sa1 in db.StudentAttributes
                                                                where sa1.StudentAttributeId == studentAttribId
                                                                select sa1).ToList<StudentAttribute>();
                    StudentAttribute sa = new StudentAttribute();
                    sa = studentAttributes.First<StudentAttribute>();

                    if (sa.AttributeExpires)
                    {
                        txtEditStudentAttributeExpiryDate.Visible = true;

                    }
                    else
                    {
                        txtEditStudentAttributeExpiryDate.Visible = false;
                    }
                }

                //Find out if there is a record of the student having this attribute


                PlacementManager.LinkUserStudentAttributeComponent oLinkStudentAttribC = new PlacementManager.LinkUserStudentAttributeComponent();
                List<PlacementManager.LinkUserStudentAttribute> listLinkUserStudentAttribute = oLinkStudentAttribC.SelectAllWUserIdStudentAttributeId(studentAttribId, Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString()), "studentAttribId");


                List<UserStudentAttribute> userStudentAttributes = (from usa1 in db.UserStudentAttributes
                                                                    where usa1.StudentAttributeId == studentAttribId
                                                                    && usa1.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                                                                    orderby usa1.StudentAttributeId
                                                                    select usa1).ToList<UserStudentAttribute>();

                if (userStudentAttributes.Count > 0)//The link exists
                {
                    UserStudentAttribute usa = new UserStudentAttribute();
                    usa = userStudentAttributes.First<UserStudentAttribute>();
                    //Let's see if is active
                    txtEditStudentAttributeExpiryDate.Text = usa.ExpiryDate.ToShortDateString();
                    if (usa.IsActive)
                    {
                        ((CheckBox)e.Row.FindControl("cbEditStudentAttribute")).Checked = true;
                    }
                }
                else
                {
                    txtEditStudentAttributeExpiryDate.Text = DateTime.Now.AddYears(1).ToShortDateString();
                }

                //Set the hidden text box to be the linkid
                ((TextBox)e.Row.FindControl("txtHiddenLinkId")).Text = studentAttribId.ToString();
            }
        }
    }
    protected void gvStudents_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.fvUser.DataBind();
        this.gvStudentAttributes.DataBind();
        this.gvNotes.DataBind();
        this.lvStudentAddresses.DataBind();
        this.gvUnits.DataBind();
        this.fvName.DataBind();
        this.gvStudentAttributes.DataBind();
        this.gvStudentPlacements.DataBind();
        this.lvUserContactNumbers.DataBind();
    }
    protected string GetFormattedAddress(string addressId)
    {
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();
        return oAddressC.GetFormattedAddress(Int32.Parse(addressId));
    }
    protected void gvStudents_DataBound(object sender, EventArgs e)
    {
        if (this.gvStudents.Rows.Count > 0)
        {
            this.lvStudentAddresses.Visible = true;
            this.lvStudentAddresses.DataBind();

            this.fvUser.Visible = true;
            this.fvUser.DataBind();

            this.gvStudentAttributes.Visible = true;
            this.gvStudentAttributes.DataBind();

            this.gvNotes.Visible = true;
            this.gvNotes.DataBind();

            this.gvUnits.Visible = true;
            ddEnrolled.DataBind();
            this.gvUnits.DataBind();

            this.fvName.Visible = true;
            this.fvName.DataBind();

            this.gvStudentPlacements.Visible = true;
            this.ddSearchCompletionStatus.Visible = true;
            this.ddSearchCompletionStatus.DataBind();
            this.gvStudentPlacements.DataBind();

            this.lvUserContactNumbers.Visible = true;
            this.lvUserContactNumbers.DataBind();

            this.SaveAttributes.Visible = true;
            this.SaveAttributes.DataBind();

            this.txtPlacementSearchCompletionStatus.Visible = true;
            this.unitsFilterLabel.Visible = true;
            this.ddEnrolled.Visible = true;
        }
        else
        {
            this.fvUser.Visible = false;
            this.gvStudentAttributes.Visible = false;
            this.gvNotes.Visible = false;
            this.lvStudentAddresses.Visible = false;
            this.gvUnits.Visible = false;
            this.fvName.Visible = false;
            this.gvStudentAttributes.Visible = false;
            this.gvStudentPlacements.Visible = false;
            this.lvUserContactNumbers.Visible = false;
            this.SaveAttributes.Visible = false;
            this.ddSearchCompletionStatus.Visible = false;

            this.txtPlacementSearchCompletionStatus.Visible = false;
            this.unitsFilterLabel.Visible = false;
            this.ddEnrolled.Visible = false;
        }

    }
    protected void imgbtnSimpleSearch_Click(object sender, ImageClickEventArgs e)
    {
        this.gvStudents.DataBind();

    }
    protected void gvNotes_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteNote")
        {
            PlacementManager.NoteComponent oNoteC = new PlacementManager.NoteComponent();
            PlacementManager.Note oNote = oNoteC.SelectOne(Int32.Parse(e.CommandArgument.ToString()));
            oNote.IsActive = false;
            oNoteC.Update(oNote);
            this.gvNotes.DataBind();

        }
    }
    protected void ldsStudentslists_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();

        var query = from u in db.Users
                    join su in db.StudentUnits on u.UserId equals su.UserId
                    where u.FirstName.StartsWith(this.txtSearchFirstName.Text)
                    && u.LastName.StartsWith(this.txtSearchLastName.Text)
                    && u.IdNumber.StartsWith(this.txtSearchStudentNumber.Text)
                    && su.UnitId == Int32.Parse(this.ddUnits.SelectedValue.ToString())
                    orderby u.FirstName
                    select u;
        e.Result = query;
    }
    protected void ldsStudentDetails_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();

        var query = from u in db.Users
                    where u.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                    orderby u.FirstName
                    select u;
        e.Result = query;

    }
    protected void ldsStudentAddresses_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();

        var query = from a in db.Addresses
                    join ua in db.UserAddresses on a.AddressId equals ua.AddressId
                    where ua.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                    && ua.IsActive == true
                    && a.IsActive == true
                    orderby ua.IsPrimary
                    select a;

        e.Result = query;
    }
    protected void lvStudentAddresses_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        string user = User.Identity.Name;

        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
        CheckBox isPrimaryCheckbox = (CheckBox)this.Find(lvStudentAddresses, "lbleditIsPrimary");
        //Validation for isPrimary

        List<UserAddress> userAddresses = (from ua in db.UserAddresses
                                           where ua.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString()) &&
                                           ua.IsPrimary == true
                                           select ua).ToList<UserAddress>();

        if (userAddresses.Count > 0 && isPrimaryCheckbox.Checked)
        {
            if (userAddresses.First<UserAddress>().AddressId != Int32.Parse(e.Keys["AddressId"].ToString()))
            {
                CustomValidator cu = (CustomValidator)this.Find(lvStudentAddresses, "EditPrimaryAddressValidator");
                cu.ErrorMessage = "There can only be one primary address!";
                cu.IsValid = false;
                e.Cancel = true;
                return;
            }
        }

        Address a = (from add in db.Addresses
                     where add.AddressId == Int32.Parse(e.Keys["AddressId"].ToString())
                     select add).ToList<Address>().First<Address>();

        a.Address1 = e.NewValues["Address1"].ToString();
        a.Address2 = e.NewValues["Address2"].ToString();
        a.Street = e.NewValues["Street"].ToString();
        a.PostCode = e.NewValues["PostCode"].ToString();
        DropDownList stateDropDown = (DropDownList)this.Find(this.lvStudentAddresses, "State");
        a.StateId = Int32.Parse(stateDropDown.SelectedValue.ToString());
        DropDownList countryDropDown = (DropDownList)this.Find(this.lvStudentAddresses, "Country");
        a.CountryId = Int32.Parse(countryDropDown.SelectedValue.ToString());

        a.DateUpdated = DateTime.Now;
        a.UpdatedBy = user;


        a.UserAddresses[0].IsPrimary = isPrimaryCheckbox.Checked;
        db.SubmitChanges();

        e.Cancel = true;

        lvStudentAddresses.EditIndex = -1;


    }
    protected void ldsStudentUnits_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
        if (ddEnrolled.SelectedValue == "-1")
        {
            var query = from su in db.StudentUnits
                        join u in db.Units on su.UnitId equals u.UnitId
                        join user in db.Users on su.UserId equals user.UserId
                        where user.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                        && u.IsActive == true
                        && su.IsActive == true
                        select new
                        {
                            UnitId = su.UnitId,
                            UnitCode = u.UnitCode,
                            UnitName = u.UnitName,
                            DateUpdated = su.DateUpdated,
                            UpdatedBy = su.UpdatedBy,
                            DateCreated = su.DateCreated,
                            CreatedBy = su.CreatedBy,
                            IsCurrentlyEnrolled = su.IsCurrentlyEnrolled
                        }
                        ;

            e.Result = query;
        }
        else
        {
            var query = from su in db.StudentUnits
                        join u in db.Units on su.UnitId equals u.UnitId
                        join user in db.Users on su.UserId equals user.UserId
                        where user.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                        && u.IsActive == true
                        && su.IsActive == true
                        && su.IsCurrentlyEnrolled ==  Boolean.Parse(ddEnrolled.SelectedValue)
                        select new
                        {
                            UnitId = su.UnitId,
                            UnitCode = u.UnitCode,
                            UnitName = u.UnitName,
                            DateUpdated = su.DateUpdated,
                            UpdatedBy = su.UpdatedBy,
                            DateCreated = su.DateCreated,
                            CreatedBy = su.CreatedBy,
                            IsCurrentlyEnrolled = su.IsCurrentlyEnrolled
                        }
                        ;

            e.Result = query;
        }


    }
    protected void gvNotes_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }
    protected void btnSaveNote_Click(object sender, EventArgs e)
    {
        if (this.gvStudents.Rows.Count > 0)
        {
            PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
            Note n = new Note();

            string user = User.Identity.Name;

            n.EntityId = Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString());
            n.NoteText = this.txtAddNewNote.Text;
            n.NoteTypeId = 2;
            n.IsActive = true;
            n.CreatedBy = user;
            n.DateCreated = DateTime.Now;
            n.UpdatedBy = user;
            n.DateUpdated = DateTime.Now;
            db.Notes.InsertOnSubmit(n);
            db.SubmitChanges();
            this.txtAddNewNote.Text = "";
            gvNotes.DataBind();

            //Update the panel as grid view doesnot refresh on addition.
            UpdatePnlNotes.Update();
        }
    }
    protected void ldsComments_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();

        var query = from n in db.Notes
                    where n.EntityId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                    && n.IsActive == true
                    orderby n.DateUpdated
                    select n;
        e.Result = query;
    }
    protected void ldsStudentAttributes_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
        var query = from sa in db.StudentAttributes
                    select new
                    {
                        StudentAttributeName = sa.StudentAttributeName,
                        StudentAttributeId = sa.StudentAttributeId
                    }
                    ;
        e.Result = query;

    }
    protected void lvStudentAddresses_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string user = User.Identity.Name;

        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();

        CheckBox isPrimaryCheckbox = (CheckBox)this.Find(this.lvStudentAddresses, "lbleditIsPrimary");

        //Validation for isPrimary
        List<UserAddress> userAddresses = (from ua1 in db.UserAddresses
                                           where ua1.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                                           select ua1).ToList<UserAddress>();
        foreach (UserAddress u in userAddresses)
        {
            if (u.IsPrimary == true && isPrimaryCheckbox.Checked)
            {
                CustomValidator cu = (CustomValidator)this.Find(lvStudentAddresses, "PrimaryAddressValidator");
                cu.ErrorMessage = "There can only be one primary address!";
                cu.IsValid = false;
                e.Cancel = true;
                return;
            }
        }

        Address a = new Address();
        a.Address1 = e.Values["Address1"].ToString();
        a.Address2 = e.Values["Address2"].ToString();
        a.City = e.Values["City"].ToString();
        DropDownList countryDropDown = (DropDownList)this.Find(this.lvStudentAddresses, "Country");
        a.CountryId = Int32.Parse(countryDropDown.SelectedValue.ToString());
        a.CreatedBy = user;
        a.DateCreated = DateTime.Now;
        a.DateUpdated = DateTime.Now;
        a.IsActive = true;
        a.IsPostal = true;
        a.Latitude = 0;
        a.Longitude = 0;
        a.PostCode = e.Values["PostCode"].ToString();
        //ToDo: StateId

        DropDownList stateDropDown = (DropDownList)this.Find(this.lvStudentAddresses, "State");
        a.StateId = Int32.Parse(stateDropDown.SelectedValue.ToString());

        a.Street = e.Values["Street"].ToString();

        a.UpdatedBy = user;

        UserAddress ua = new UserAddress();
        ua.CreatedBy = user;
        ua.DateCreated = DateTime.Now;
        ua.DateUpdated = DateTime.Now;
        ua.IsActive = true;
        ua.IsPrimary = isPrimaryCheckbox.Checked;
        ua.UpdatedBy = user;
        ua.UserId = (int)gvStudents.SelectedDataKey.Value;

        a.UserAddresses.Add(ua);

        db.Addresses.InsertOnSubmit(a);
        db.SubmitChanges();

        int newAddressId = a.AddressId;
        e.Cancel = true;

        lvStudentAddresses.DataBind();
    }


    /// <summary>
    /// This method is written as some if the the child control could not be found using findcontrol.
    /// Might be a bug.
    /// </summary>
    /// <param name="control"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    private Control Find(Control control, string id)
    {
        if (control.ID == id)
        {
            return control;
        }
        foreach (Control child in control.Controls)
        {
            Control result = this.Find(child, id);
            if (result != null)
            {
                return result;
            }
        }
        return null;
    }
    protected void SaveAttributes_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gvStudentAttributes.Rows.Count; i++)
        {
            gvStudentAttributes_RowCommand(i);
        }

        this.gvStudentAttributes.DataBind();
    }
    protected void ldsStudentPlacements_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
        if (ddSearchCompletionStatus.SelectedValue == "-1")
        {
            var query = from a in db.Allocations
                        // join pt in db.PlacementTypes on a.PlacementTypeId equals pt.PlacementTypeId 
                        // join vpt in db.VenuePlacementTypes on a.VenuePlacementTypeId equals vpt.VenuePlacementTypeId
                        // join v in db.Venues on vpt.VenueId equals v.VenueId

                        where a.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                        select new
                        {
                            AllocationId = a.AllocationId,
                            VenueName = a.VenuePlacementType.Venue.VenueName,
                            UnitCode = a.PlacementType.Unit.UnitCode,
                            CampusCode = a.PlacementType.Campus.CampusCode,
                            SemesterId = a.PlacementType.SemesterId,
                            Year = a.PlacementType.Year,
                            DateStart = a.PlacementType.DateStart,
                            DateEnd = a.PlacementType.DateEnd,
                            CompletionStatus = a.CompletionStatus.CompletionStatusName,
                            CompletionStatusId = a.CompletionStatusId
                        }
                        ;

            e.Result = query;
        }
        else
        {
            var query = from a in db.Allocations
                        // join pt in db.PlacementTypes on a.PlacementTypeId equals pt.PlacementTypeId 
                        // join vpt in db.VenuePlacementTypes on a.VenuePlacementTypeId equals vpt.VenuePlacementTypeId
                        // join v in db.Venues on vpt.VenueId equals v.VenueId

                        where a.UserId == Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString())
                         && a.CompletionStatusId == Int32.Parse(ddSearchCompletionStatus.SelectedValue)
                        select new
                        {
                            AllocationId = a.AllocationId,
                            VenueName = a.VenuePlacementType.Venue.VenueName,
                            UnitCode = a.PlacementType.Unit.UnitCode,
                            CampusCode = a.PlacementType.Campus.CampusCode,
                            SemesterId = a.PlacementType.SemesterId,
                            Year = a.PlacementType.Year,
                            DateStart = a.PlacementType.DateStart,
                            DateEnd = a.PlacementType.DateEnd,
                            CompletionStatus = a.CompletionStatus.CompletionStatusName,
                            CompletionStatusId = a.CompletionStatusId
                        }
                       ;

            e.Result = query;
        }

    }
    protected void gvStudentPlacements_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddPlacements = (DropDownList)this.Find(gvStudentPlacements, "ddCompletionStatus");

        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();
        Allocation allocation = (from a in db.Allocations
                                 where a.AllocationId == Int32.Parse(e.Keys["AllocationId"].ToString())
                                 select a).ToList<Allocation>().First<Allocation>();
        allocation.CompletionStatusId = Int32.Parse(ddPlacements.SelectedValue.ToString());
        db.SubmitChanges();
        gvStudentPlacements.EditIndex = -1;
        gvStudentPlacements.DataBind();
        UpdatepnlPlacements.Update();
        e.Cancel = true;
    }
    protected void ddSearchCompletionStatus_DataBinding(object sender, EventArgs e)
    {
    }
    protected void ddSearchCompletionStatus_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("All", "-1", true);
        ddSearchCompletionStatus.Items.Add(li);
        ddSearchCompletionStatus.SelectedValue = "-1";
    }
    protected void ddSearchCompletionStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvStudentPlacements.DataBind();
    }
    protected void lvUserContactNumbers_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string user = User.Identity.Name;

        PlacementManagerLinqDataContext db = new PlacementManagerLinqDataContext();

        UserContactNumber ucn = new UserContactNumber();
        ucn.UserId = Int32.Parse(this.gvStudents.SelectedDataKey.Value.ToString());
        ucn.IsActive = true;
        ucn.CreatedBy = user;
        ucn.DateCreated = DateTime.Now;
        ucn.DateUpdated = DateTime.Now;
        ucn.UpdatedBy = user;

        ContactNumber c = new ContactNumber();
        //DropDown Contact Number Type
        DropDownList contactNumberType = (DropDownList)this.Find(lvUserContactNumbers, "ContactTypeDropDown");
        c.ContactNumberTypeId = Int32.Parse(contactNumberType.SelectedValue.ToString());

        c.ContactNumberValue = e.Values["ContactNumberValue"].ToString();
        c.UserContactNumbers.Add(ucn);
        db.ContactNumbers.InsertOnSubmit(c);
        db.SubmitChanges();

        lvUserContactNumbers.DataBind();

        e.Cancel = true;
    }
    protected void gvStudents_PageIndexChanged(object sender, EventArgs e)
    {
        this.gvStudents.SelectedIndex = 0;
    }
    protected void ddEnrolled_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvUnits.DataBind();
    }
}
